exports.addBargainActivity = function (req, res) {

    //获取restful参数
    var bodyParams = req.routeInfo.bodyParams;

    //最终结果
    var resultData = {};

    var new_oaid = 0;

    var completeCount = 0;

    var successCount = 0;

    var cash_coupon_id = 0;

    //任务流创建
    async.waterfall([
        //1.权限验证
        /*function (cb) {
            P.permissionValidation(req, res, [CORE_APIConfig.coreAPI.addOrganizationActivity], function () {
                cb()
            });
        },*/

            //添加活动基本信息
            function (cb) {
            	console.log(11111111111111111)
            	console.log(bodyParams.most_price)
            	console.log(bodyParams.least_price)
            	console.log(11111111111111111)

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.addOrganizationActivity,
                        method: 'POST',
                        data: {
                            oa_title        : bodyParams.activity_name,
                            oa_type         : "2",
                            oa_oid          : bodyParams.oid,
                            //oa_category_text: bodyParams.category_text,
                            oa_start_time   : bodyParams.activity_start_time,
                            oa_end_time     : bodyParams.activity_end_time,
                            oa_explain      : bodyParams.activity_note,
                            oa_title_image  : bodyParams.activity_image,
                            temp_most_price       : bodyParams.most_price,   //最高价格
                            temp_least_price      : bodyParams.least_price,   //最低价格
                            oa_category_text:bodyParams.oa_category_text == undefined ? "" : bodyParams.oa_category_text,
                            oa_title_image_text: bodyParams.oa_title_image_text == undefined ? "" : bodyParams.oa_title_image_text,
                            oa_title_image_text_style: bodyParams.oa_title_image_text_style == undefined ? "" : bodyParams.oa_title_image_text_style,
                            oa_skin_id: bodyParams.oa_skin_id == undefined ? "" : bodyParams.oa_skin_id,
                            oa_music_id: bodyParams.oa_music_id == undefined ? "" : bodyParams.oa_music_id,
                            oa_class_time: bodyParams.oa_class_time == undefined ? "" : bodyParams.oa_class_time,
                            oa_class_duration: bodyParams.oa_class_duration == undefined ? "" : bodyParams.oa_class_duration,
                            oa_class_apply: bodyParams.oa_class_apply == undefined ? "" : bodyParams.oa_class_apply,
                            oa_class_people: bodyParams.oa_class_people == undefined ? "" : bodyParams.oa_class_people,
                            oa_class_basics: bodyParams.oa_class_basics == undefined ? "" : bodyParams.oa_class_basics,
                            oa_class_phone: bodyParams.oa_class_phone == undefined ? "" : bodyParams.oa_class_phone,
                            oa_thumbnail: bodyParams.oa_thumbnail == undefined ? "" : bodyParams.oa_thumbnail,
                            oa_title_image_type: bodyParams.oa_title_image_type == undefined ? "" : bodyParams.oa_title_image_type,
                        }
                    },
                    function (err, data) {
                          console.log(err);
                          console.log(data);
                        //判断是否有错误信息
                        if (err) {
                            return RES.response(res, false, "活动新增失败，请重新尝试1");
                        }

                        new_oaid = data;

                        cb();
                    }
                );

            },

            //添加活动扩展信息
            function(cb){
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.addBargainActivityConfig,
                        method: 'POST',
                        data: {
                            bc_most_price       : bodyParams.most_price,   //最高价格
                            bc_least_price      : bodyParams.least_price,   //最低价格
                            bc_most_cut_price   : bodyParams.most_cut_price,   //砍掉的最高价格
                            bc_oaid             : new_oaid,
                            bc_least_cut_price  : bodyParams.least_cut_price, //砍掉的最低价格
                            bc_interval_cut_time: bodyParams.interval_cut_time, // 给自己砍价的 时间间隔
                            bc_invite_reward    : bodyParams.invite_reward,       //邀请获得的奖励
                            bc_appointment_info : bodyParams.appointment_info,  //预约信息
                            bc_class_adjust_info: bodyParams.class_adjust_info, // 调课信息
                            bc_refund_info      : bodyParams.refund_info,   // 退款说明
                            bc_other_info       : bodyParams.other_info,    // 其他说明
                            bc_cozy_tips        : bodyParams.cozy_tips,     // 温馨提示
                            bc_get_reward       : bodyParams.get_reward,       //领取奖励方式
                            bc_preferential     : bodyParams.preferential,      //门店优惠信息每条以 | 隔开
                            bc_course_title     : bodyParams.course_title,
                            bc_course_image     : bodyParams.course_image,
                            bc_course_price     : bodyParams.course_price,
                            bc_course_count     : bodyParams.course_count,
                            bc_coupon_id        : bodyParams.bc_coupon_id == undefined ? "" : bodyParams.bc_coupon_id,
                            bc_coupon_type        : bodyParams.bc_coupon_type == undefined ? "" : bodyParams.bc_coupon_type,
                            bc_last_update_time :Date.parse(new Date())*0.001,
                            bc_max_user_count   :  bodyParams.bc_max_user_count == undefined ? "" : bodyParams.bc_max_user_count // 砍价活动最大人数限制 0为不限制
                        }
                    },
                    function (err, data) {

                        //  console.log(data);

                        //判断是否有错误信息
                        if (err) {
                            return RES.response(res, false, "活动新增失败，请重新尝试2");
                        }

                        cb();
                    }


                );

            },

            //添加发送代金券的记录
            function(cb){
            if(bodyParams.bc_coupon_id != undefined && bodyParams.bc_coupon_id !=''&& bodyParams.bc_coupon_id !=0){
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.addActivityCoupon,
                        method: 'POST',
                        data: {
                            ac_oid       : bodyParams.oid,                //机构id
                            ac_oaid      : new_oaid,                      //活动id
                            ac_type      : bodyParams.bc_coupon_type,   //活动id
                            ac_fid       : bodyParams.bc_coupon_id,      //券id
                            ac_oatype    : 1     //券id
                        }
                    },
                    function (err, data) {

                        //  console.log(data);

                        //判断是否有错误信息
                        if (err) {
                            return RES.response(res, false, "活动奖励记录添加失败");
                        }

                        cb();
                    }
                );
            }else{
                cb();
            }

            },


            //增加活动内容

            function(cb){



                var contentArr =  JSON.parse(bodyParams.activity_content_data);

                successCount  = contentArr.length;

                completeCount = 0;
                //创建添加任务
                var tasks = [];

                //console.log(contentArr);

                //循环节点列表
                contentArr.forEach(function (item, i) {
                    //创建每一个插入任务
                    tasks.push(function (cb) {

                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.addBargainActivityContent,
                                method: 'POST',
                                data: {
                                    bc_type      : item.type,
                                    bc_order     : i,
                                    bc_content   : item.content,
                                    bc_oaid      : new_oaid
                                }
                            },
                            function (err, data) {

                                //console.log(data);

                                //判断是否有错误信息
                                if(err) {
                                    //console.log("添加错误");
                                }else{
                                    completeCount++;
                                }

                                cb();
                            }
                        );
                    });
                });
                async.waterfall(tasks, cb);
            },

            //判断写入结果
            function(cb){

                if(successCount == completeCount){
                    cb();
                }else{
                    return RES.response(res, false, "活动新增失败，请重新尝试");
                }
            }

        ], function (err, result) {
            //返回最终拼接结果
            RES.response(res, true, resultData);
        }
    );
}